package com.huanxing.cloud.mall.api.event.listener;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huanxing.cloud.common.core.constant.CommonConstants;
import com.huanxing.cloud.mall.api.event.HxOrderRefundEvent;
import com.huanxing.cloud.mall.api.service.IGoodsSkuService;
import com.huanxing.cloud.mall.api.service.IOrderInfoService;
import com.huanxing.cloud.mall.api.service.IOrderItemService;
import com.huanxing.cloud.mall.api.service.IOrderRefundService;
import com.huanxing.cloud.mall.common.entity.OrderInfo;
import com.huanxing.cloud.mall.common.entity.OrderItem;
import com.huanxing.cloud.mall.common.entity.OrderRefund;
import com.huanxing.cloud.mall.common.enums.OrderItemEnum;
import com.huanxing.cloud.mall.common.enums.OrderRefundEnum;
import com.huanxing.cloud.mall.common.enums.OrderStatusEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: lijx
 * @date: 2023/4/24 11:57
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class HxOrderRefundEventListener {

	private final IOrderRefundService orderRefundService;

	private final IOrderItemService orderItemService;

	private final IGoodsSkuService goodsSkuService;

	private final IOrderInfoService orderInfoService;

	@EventListener(HxOrderRefundEvent.class)
	public void refundStatusEventListener(HxOrderRefundEvent event) {
		// 退款单信息
		final OrderRefund orderRefund = event.getOrderRefund();
		if (null == orderRefund) {
			log.warn("orderRefund not found!");
			return;
		}
		orderRefund.setArrivalStatus(CommonConstants.YES);
		orderRefundService.updateById(orderRefund);
	}

	@EventListener(HxOrderRefundEvent.class)
	public void stockEventListener(HxOrderRefundEvent event) {
		// 退款单信息
		final OrderRefund orderRefund = event.getOrderRefund();
		if (null == orderRefund) {
			log.warn("orderRefund not found!");
			return;
		}
		if (!OrderRefundEnum.STATUS_12.getCode().equals(orderRefund.getStatus())) {
			return;
		}
		final OrderItem orderItem = event.getOrderItem();
		goodsSkuService.updateStock(orderItem.getSkuId(), orderItem.getBuyQuantity());
	}

	@EventListener(HxOrderRefundEvent.class)
	public void orderStatusEventListener(HxOrderRefundEvent event) {
		// 退款单信息
		final OrderRefund orderRefund = event.getOrderRefund();
		if (null == orderRefund) {
			log.warn("orderRefund not found!");
			return;
		}
		OrderItem orderItem = event.getOrderItem();
		orderItem.setIsRefund(CommonConstants.YES);
		orderItem.setStatus(OrderRefundEnum.STATUS_11.getCode().equals(orderRefund.getStatus())
				? OrderItemEnum.STATUS_3.getCode() : OrderRefundEnum.STATUS_12.getCode().equals(orderRefund.getStatus())
						? OrderItemEnum.STATUS_4.getCode() : "");
		orderItemService.updateById(orderItem);

		// 查询所有子订单
		List<OrderItem> orderItemList = orderItemService
			.list(Wrappers.<OrderItem>lambdaQuery().eq(OrderItem::getOrderId, orderRefund.getOrderId()))
			.stream()
			.filter(orderItem1 -> !orderItem1.getId().equals(orderItem.getId())
					&& CommonConstants.NO.equals(orderItem1.getIsRefund()))
			.collect(Collectors.toList());

		// 判断是否全部完成退款
		if (CollectionUtils.isEmpty(orderItemList)) {
			// 取消订单
			OrderInfo orderInfo = new OrderInfo();
			orderInfo.setId(orderRefund.getOrderId());
			orderInfo.setStatus(OrderStatusEnum.STATUS_11.getCode());
			orderInfoService.updateById(orderInfo);
		}
	}

}
